package com.sduept.nwld.dataserver.service.equipment.assessment.rest;

import com.sduept.core.dao.SuperDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 平均分统计分析
 * @author huangyu
 * @Date 2020年4月9日
 */
@RestController
@RequestMapping("/grade")
public class GradeAnalysisService {
	
	@Autowired
	private SuperDAO dao;
	
	/**
	 * 统计各个分子公司的检查站的平均扣分值
	 * @return
	 */

	@GetMapping("getAverageGradeGroupByAcceptScope")
	public List<AverageGradeDTO> getAverageGradeGroupByAcceptScope() {
		List<AverageGradeDTO> results = new ArrayList<AverageGradeDTO>();
		String sql = "SELECT t.acceptScope, ROUND(AVG(t.deduct),2) FROM t_inspection_station t WHERE t.status=2 GROUP BY t.acceptScope";
		List<?> lt = dao.nativeQuery(sql);
		for (Object object : lt) {
			Object[] objs = (Object[]) object;
			AverageGradeDTO dto = new AverageGradeDTO();
			dto.setDept(objs[0]!=null?objs[0].toString():null);
			dto.setAvgGrade(objs[1]!=null?objs[1].toString():null);
			results.add(dto);
		}
		sortByGrade(results);
		return results;
	}
	
	/**
	 * 统计指定分子公司下各个供电局的扣分情况
	 * @return
	 */

	@GetMapping("getAverageGradeGroupByAcceptUnit/{acceptScope}")
	public List<AverageGradeDTO> getAverageGradeGroupByAcceptUnit(@PathVariable("acceptScope")String acceptScope) {
		List<AverageGradeDTO> results = new ArrayList<AverageGradeDTO>();
		String sql = "SELECT t.acceptUnit, ROUND(AVG(t.deduct),2) FROM t_inspection_station t WHERE t.status=2 AND t.acceptScope='"+acceptScope+"' GROUP BY t.acceptUnit";
		List<?> lt = dao.nativeQuery(sql);
		for (Object object : lt) {
			Object[] objs = (Object[]) object;
			AverageGradeDTO dto = new AverageGradeDTO();
			dto.setDept(objs[0]!=null?objs[0].toString():null);
			dto.setAvgGrade(objs[1]!=null?objs[1].toString():null);
			results.add(dto);
		}
		sortByGrade(results);
		return results;
	}
	
	private void sortByGrade(List<AverageGradeDTO> list) {
		Collections.sort(list,new Comparator<AverageGradeDTO> () {
			@Override
			public int compare(AverageGradeDTO o1, AverageGradeDTO o2) {
				float f1 = Float.parseFloat(o1.getAvgGrade());
				float f2 = Float.parseFloat(o2.getAvgGrade());
				if(f1<f2) {
					return -1;
				} else if (f1>f2) {
					return 1;
				} else {
					return 0;
				}
			}
		});
	}
	
	class AverageGradeDTO {
		/**
		 * 统计依据（分子公司、供电局）
		 */
		private String dept;
		
		/**
		 * 平均扣分
		 */
		private String avgGrade;

		public String getDept() {
			return dept;
		}

		public String getAvgGrade() {
			return avgGrade;
		}

		public void setDept(String dept) {
			this.dept = dept;
		}

		public void setAvgGrade(String avgGrade) {
			this.avgGrade = avgGrade;
		}
		
	}
}
